/* AI.C */

/*
	Copyright (C) 2010 by Lesnikov Andrey <ozkriff@gmail.com>
	This file is part of ozkriff_game_2.

	ozkriff_game_2 is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	ozkriff_game_2 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with ozkriff_game_2.  If not, see <http://www.gnu.org/licenses/>.

	See the COPYING file for more details.
*/

#include "SDL/SDL.h"
#include "SDL/SDL_net.h"
#include <math.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "defines.h"
#include "nograph.h"
#include "path.h"


extern bool	IS_PROGRAM_LOOPING;
extern uint8_t	NET_MODE;
extern unit*	UNITS;
extern uint	PLAYER;
extern cell	MAP[MAP_SIZE_X][MAP_SIZE_Y];
extern int	MODE;
extern move	MOVE;
extern char *	SERVER;
extern TCPsocket SOCKET;



unit *U=NULL;

void
do_AI()
{
	if(NET_MODE == MODE_PASSIVE)
		return;

	//unit *u = UNITS;
	if(U == NULL)
		U = UNITS;

	unit *u = U;
	while(u != NULL)
	{
		if(u->player == PLAYER)
		{
			fill_map(u);
			
			ushort aX, aY, i;
			for(i=rand()%4; i<6;i++)
				if(get_close_hex_pos(u->mx, u->my, i, &aX, &aY))
					if(MAP[aX][aY].unit == NULL && MAP[aX][aY].move_cost <= u->ap)
						break;
			if(i == 6) exit(666);

			//while(!get_close_hex_pos(u->mx, u->my, rand()%6, &aX, &aY));

			//if(MAP[aX][aY].unit == NULL && MAP[aX][aY].move_cost <= u->ap)
			{
				//if character has requested action points
				u->ap -= MAP[aX][aY].move_cost;
				get_path(u, aX, aY);
				MODE = MODE_MOVE;
				MOVE.step_count = 0;
				MOVE.unit =u;
				MAP[u->mx][u->my].unit = NULL;
				
				send_to_server(NET_MOVE);
				send_to_server(u->mx);
				send_to_server(u->my);
				send_to_server(aX);
				send_to_server(aY);

				U = U->next;
				end_player_turn();
				return;
			}
		}
		u = u->next;
	}

	end_player_turn();
}


void
hanlde_events()
{
	SDL_Event E;

	while(SDL_PollEvent(&E)) switch(E.type)
	{
		default:
			//puts("unknown event!");
			break;
			
		case SDL_QUIT:
			IS_PROGRAM_LOOPING = false;
			break;
	}
}



void
init_sdl()
{
	SDL_Init(SDL_INIT_TIMER);
	atexit(SDL_Quit);
}



void
do_init()
{
	srand(time(0));

	init_net();
	init_sdl();
	init_q();
	generate_hex();
	init_map(); //generate map and units information
}


#undef main //write straight to the cosole

int
main(int argc, char **argv)
{
	if(argc == 1) exit(0);
	PLAYER = atoi(argv[1]);
	if(argc == 3)
		SERVER = argv[2];

	do_init();
	
	while(IS_PROGRAM_LOOPING)
	{
		do_net();
		hanlde_events();
		do_logic();
		change_frames();
		do_AI();
	}
	
	send_to_server(NET_QUIT);
	
	return(0);
}
